<template>
    <f7-page>
        <f7-navbar>
            <f7-nav-left>
                <f7-link @click="back">
                    <f7-icon icon="icon-back" style="margin-left:15px;"></f7-icon>
                </f7-link>
            </f7-nav-left>
            <f7-nav-title title=问题登记></f7-nav-title>
        </f7-navbar>
        <f7-list inline-labels no-hairlines inset style="margin-top:10px;">
            <f7-list-item>
                <TakePhoto ref="refTakePhoto" @photoAdd="photoAdd" @photoDelete="photoDelete"></TakePhoto>
            </f7-list-item>
            <f7-list-item link="#" @click="$f7.popup.open('#selectLocationPopup', true);">
                <f7-label style="width:30%;">部位&nbsp;&nbsp;
                    <span v-if="form.houseId != null"
                        @click.stop="openHousePoint($store.state.global.houseMap[form.houseId])">
                        <f7-badge color="blue">图</f7-badge>
                    </span>
                </f7-label>
                <f7-list-item-cell>{{place}}</f7-list-item-cell>
            </f7-list-item>
            <f7-list-item chevron-center link="#"  @click="$f7.popup.open('#selectCheckItemPopup', true);">
                <f7-label style="width:30%;">检查项</f7-label>
                <f7-list-item-cell>
                <span>
                    {{form.problemClassId == null ? '请选择' :
                    $store.state.global.problemClassMap[
                        $store.state.global.problemClassMap[form.problemClassId].parent_problem_class_id
                    ].problem_class_name}}
                </span>
                <span v-if="form.problemClassId != null">
                    <br/>
                    {{$store.state.global.problemClassMap[form.problemClassId].problem_class_name}}
                </span>
                </f7-list-item-cell>
            </f7-list-item>
            <f7-list-item link="#" v-show="form.problemId != null" @click="$f7.popup.open('#selectProblemPopup', true)">
                <f7-label style="width:30%;">描述</f7-label>
                <f7-list-item-cell v-if="form.problemId != null">
                    {{$store.state.global.problemMap[form.problemId].problem_name}}
                </f7-list-item-cell>
            </f7-list-item>
            <f7-list-item>
                <f7-input type="textarea" :value="form.remark" label="补充说明" placeholder="补充说明"
                    @change="form.remark = $event.target.value">
                </f7-input>
            </f7-list-item>
            <f7-list-item v-show="!isProblemClassHighlight">
                <f7-label style="width:30%;">严重程度</f7-label>
                <f7-segmented raised tag="span">
                    <f7-button style="width:70px;"
                        :active="form.emergencyDegree == '一般'"
                        @click="form.emergencyDegree = '一般'">一般
                    </f7-button>
                    <f7-button style="width:70px;"
                        :active="form.emergencyDegree == '紧急'"
                        @click="form.emergencyDegree = '紧急'">紧急
                    </f7-button>
                    <f7-button style="width:70px;"
                        :active="form.emergencyDegree == '严重'"
                        @click="form.emergencyDegree = '严重'">严重
                    </f7-button>
                </f7-segmented>
            </f7-list-item>
            <f7-list-item v-show="!isProblemClassHighlight">
                <f7-label style="width:30%;">整改期限</f7-label>
                <f7-stepper :value="form.repairDeadline" input-readonly :step="1"
                    @stepper:minusclick="form.repairDeadline <= 1 ? 1 : form.repairDeadline--"
                    @stepper:plusclick="form.repairDeadline >= 99 ? 99 : form.repairDeadline++">
                </f7-stepper>
            </f7-list-item>
            <f7-list-item v-show="!isProblemClassHighlight" id="repairUserSmartSelect" smart-select
                :smart-select-params="{openIn: 'popup', closeOnSelect: true, searchbar: true, searchbarPlaceholder: '搜索', searchbarDisableText: '取消', popupCloseLinkText: '返回'}">
                <f7-label>整改人</f7-label>
                <select v-model="form.repair">
                    <optgroup label="施工单位">
                        <option v-for="(userId, i) in contractors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                    <optgroup label="监理单位">
                        <option v-for="(userId, i) in supervisors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                    <optgroup label="建设单位">
                        <option v-for="(userId, i) in constructors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                </select>
            </f7-list-item>
            <f7-list-item v-show="!isProblemClassHighlight" id="repairContractorSmartSelect" smart-select
                :smart-select-params="{openIn: 'popup', closeOnSelect: true, searchbar: true, searchbarPlaceholder: '搜索', searchbarDisableText: '取消', popupCloseLinkText: '返回'}">
                <f7-label>责任单位</f7-label>
                <select v-model="form.contractorId">
                    <option v-for="(contractor, key, i) in $store.state.global.contractorMap" 
                        v-if="isShowContractor(contractor)" :key="contractor.id"
                        :value="contractor.id" v-text="contractor.contractor_short_name">
                    </option>
                </select>
            </f7-list-item>
            <f7-list-item v-show="!isProblemClassHighlight" id="reinspectUserSmartSelect" smart-select
                :smart-select-params="{openIn: 'popup', searchbar: true, searchbarPlaceholder: '搜索', searchbarDisableText: '取消', popupCloseLinkText: '返回'}">
                <f7-label>复验人</f7-label>
                <select multiple v-model="form.reinspects">
                    <optgroup label="施工单位">
                        <option v-for="(userId, i) in contractors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                    <optgroup label="监理单位">
                        <option v-for="(userId, i) in supervisors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                    <optgroup label="建设单位">
                        <option v-for="(userId, i) in constructors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                </select>
            </f7-list-item>
            <f7-list-item id="ccUserSmartSelect" smart-select
                :smart-select-params="{openIn: 'popup', searchbar: true, searchbarPlaceholder: '搜索', searchbarDisableText: '取消', popupCloseLinkText: '返回'}">
                <f7-label>抄送人</f7-label>
                <select multiple v-model="form.ccs">
                    <optgroup label="施工单位">
                        <option v-for="(userId, i) in contractors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                    <optgroup label="监理单位">
                        <option v-for="(userId, i) in supervisors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                    <optgroup label="建设单位">
                        <option v-for="(userId, i) in constructors" :key="userId"
                            :value="userId" v-text="$store.state.global.userMap[userId].realname">
                        </option>
                    </optgroup>
                </select>
            </f7-list-item>
            <f7-list-item>
                <f7-list-button title="暂存" style="width:100%;" @click="saveTemporary">
                </f7-list-button>
                <f7-list-button title="提交" style="width:100%;" @click="submit" v-show="isOnline">
                </f7-list-button>
            </f7-list-item>
        </f7-list>
        <f7-popup id="selectLocationPopup" :backdrop="false" :close-by-backdrop-click="false">
            <f7-page>
                <f7-navbar>
                    <f7-nav-left>
                        <f7-link href="#" @click="$f7.popup.close('#selectLocationPopup', true);">
                            <i class="icon material-icons" style="font-size:40px;">chevron_left</i>
                        </f7-link>
                    </f7-nav-left>
                    <f7-nav-title title="选择部位"></f7-nav-title>
                </f7-navbar>
                <f7-block strong inset>
                    <f7-block-header>楼栋</f7-block-header>
                    <f7-row v-for="(building, i) in buildings"
                        v-if="i % 4 == 0" :key="building.id" style="margin-bottom:5px;">
                        <f7-col v-for="j in 4" v-if="i + j - 1 < buildings.length"
                            :key="buildings[i + j - 1].id">
                            <f7-button @click="changeBuilding(buildings[i + j - 1].id)"
                                :fill="form.buildingId == buildings[i + j - 1].id">
                                {{buildings[i + j - 1].building_name}}
                            </f7-button>
                        </f7-col>
                    </f7-row>
                </f7-block>
                <f7-block strong inset v-if="form.buildingId != null">
                    <f7-block-header>单元</f7-block-header>
                    <f7-row v-for="(unit, i) in units"
                        v-if="i % 4 == 0" :key="unit.id" style="margin-bottom:5px;">
                        <f7-col v-for="j in 4" v-if="i + j - 1 < units.length"
                            :key="units[i + j - 1].id">
                            <f7-button @click="form.unitId = units[i + j - 1].id"
                                :fill="form.unitId == units[i + j - 1].id">
                                {{units[i + j - 1].unit_name}}单元
                            </f7-button>
                        </f7-col>
                    </f7-row>
                </f7-block>
                <f7-block strong inset v-if="form.unitId != null">
                    <f7-block-header>楼层</f7-block-header>
                    <f7-row v-for="(floor, i) in floors"
                        v-if="i % 4 == 0" :key="floor.id" style="margin-bottom: 5px;">
                        <f7-col v-for="j in 4" v-if="i + j - 1 < floors.length"
                            :key="floors[i + j - 1].id">
                            <f7-button @click="form.floorId = floors[i + j - 1].id"
                                :fill="form.floorId == floors[i + j - 1].id">
                                {{floors[i + j - 1].floor_name}}层
                            </f7-button>
                        </f7-col>
                    </f7-row>
                </f7-block>
                <f7-block strong inset v-if="form.floorId != null">
                    <f7-block-header>房间</f7-block-header>
                    <f7-row v-for="(house, i) in houses"
                        v-if="i % 4 == 0" :key="house.id" style="margin-bottom: 5px;">
                        <f7-col v-for="j in 4" v-if="i + j - 1 < houses.length"
                            :key="houses[i + j - 1].id">
                            <f7-button @click="form.houseId = houses[i + j - 1].id"
                                :fill="form.houseId == houses[i + j - 1].id">
                                {{houses[i + j - 1].house_name}}
                            </f7-button>
                        </f7-col>
                    </f7-row>
                </f7-block>
            </f7-page>
        </f7-popup>
        <f7-popup id="selectCheckItemPopup" :backdrop="false" :close-by-backdrop-click="false">
            <f7-page>
                <f7-navbar>
                    <f7-nav-left>
                        <f7-link href="#" @click="$f7.popup.close('#selectCheckItemPopup', true);">
                            <i class="icon material-icons" style="font-size:40px;">chevron_left</i>
                        </f7-link>
                    </f7-nav-left>
                    <f7-nav-title title="选择检查项"></f7-nav-title>
                </f7-navbar>
                <f7-list accordion-list inset>
                    <f7-list-item accordion-item
                        v-for="(rootProblemClassCheckItem, i) in checkItems"
                        :title="rootProblemClassCheckItem.name"
                        :key="rootProblemClassCheckItem.id">
                        <f7-accordion-content>
                            <f7-list accordion-list>
                                <f7-list-item v-for="(problemClassCheckItem, j) in rootProblemClassCheckItem.children"
                                    :accordion-item="problemClassCheckItem.children.length > 0"
                                    :radio="problemClassCheckItem.children.length == 0"
                                    :title="'　　'  +  problemClassCheckItem.name"
                                    :checked="form.problemClassId == problemClassCheckItem.id"
                                    @click="form.problemClassId = problemClassCheckItem.id"
                                    :key="problemClassCheckItem.id">
                                    <f7-accordion-content v-if="problemClassCheckItem.children.length > 0">
                                        <f7-list>
                                            <f7-list-item v-for="(problemCheckItem, i) in problemClassCheckItem.children"
                                                :title="'　　　　' + problemCheckItem.name"
                                                :radio="true" :key="problemCheckItem.id"
                                                :checked="form.problemId == problemCheckItem.id"
                                                @click="form.problemId = problemCheckItem.id">
                                            </f7-list-item>
                                        </f7-list>
                                    </f7-accordion-content>
                                </f7-list-item>
                            </f7-list>
                        </f7-accordion-content>
                    </f7-list-item>
                </f7-list>
            </f7-page>
        </f7-popup>
        <f7-popup id="selectProblemPopup" :backdrop="false" :close-by-backdrop-click="false">
            <f7-page>
                <f7-navbar>
                    <f7-nav-left>
                        <f7-link href="#" @click="$f7.popup.close('#selectProblemPopup', true);">
                            <i class="icon material-icons" style="font-size:40px;">chevron_left</i>
                        </f7-link>
                    </f7-nav-left>
                    <f7-nav-title title="描述"></f7-nav-title>
                </f7-navbar>
                <f7-list inset v-if="form.problemClassId != null">
                    <f7-list-item v-for="(problemCheckItem, i) in checkItemMap[form.problemClassId].children"
                        :title="problemCheckItem.name"
                        :key="problemCheckItem.id" :radio="true"
                        :checked="form.problemId == problemCheckItem.id"
                        @click="form.problemId = problemCheckItem.id">
                    </f7-list-item>
                </f7-list>
            </f7-page>
        </f7-popup>
    </f7-page>
</template>

<script>
import networkEvent from '@/mixins/networkEvent';
import back from '@/mixins/back.js';
import userCategory from '@/mixins/userCategory.js';
import Common from "@/utils/Common.js";
import DateUtil from "@/utils/DateUtil.js";
import FileUtil from "@/utils/FileUtil.js";
import $ from 'jquery';
import events from 'events';
import store from '@/store'
import TakePhoto from "@/components/TakePhoto.vue";
import { getUser, setUser, clearUser } from '@/utils/auth';
import { getStore, setStore, clearStore } from '@/utils/storage';
import keys from '@/utils/rememberKeys';
import request from '@/utils/request';
import db from '@/utils/db';
import { upload, uploadImage } from '@/utils/upload';
import eventBus from '@/utils/eventBus.js';
import localeCheck from '@/mixins/localeCheck.js';
export default {
    mixins: [networkEvent, back, userCategory, localeCheck],
    components: {
        TakePhoto
    },
    props: {
    	batch: {
    		type: Object
    	},
    	draftProblem: {
    		type: Object
    	}
    },
    store: store,
    data: function() {
        return {
        	checkItemMap: {},
        	defaultCcs: [],
        	form: {
        		tid: null,
                batchId: null,
                buildingId: null,
                unitId: null,
                floorId: null,
                houseId: null,
                problemClassId: null,
                problemId: null,
                remark: '',
                emergencyDegree: '一般',
                repairDeadline: 0,
                repair: '',
                contractorId: '',
                reinspects: [],
                ccs: [],
                photos: [],
                problemValues: [],
                regist: ''
        	},
        	deletePhotos: []
        };
    },
    methods: {
    	isShowContractor: function(contractor) {
    		let contractorId = contractor.id;
    		let bidSections = this.$store.state.global.bidSectionContractorRel.bidSectionMap[contractorId];
    		return bidSections == null ? false : bidSections.indexOf(this.batch.bidSectionId) > -1;
    	},
    	photoAdd: function(filePath) {
    		this.form.photos.push(filePath);
    	},
    	photoDelete: function(filePath) {
    		for(var i = 0; i < this.form.photos.length; i++) {
    			if(filePath == this.form.photos[i]) {
    				this.form.photos.splice(i, 1);
                    if(filePath.indexOf(Common.directory().localeCheck) > -1) {
                        this.deletePhotos.push(filePath);
                    }
    				break;
    			}
    		}
    	},
    	openHousePoint: function(house) {
            var self = this;
            var houseType = this.$store.state.global.houseTypeMap[house.house_type_id];
            self.$f7.popup.close('#selectLocationPopup', true);
            var router = self.$f7router.navigate('/HousePoint', {
                props: {
                    imageUrl: houseType.url,
                    pointCount: 20,
                    initialPoints: self.form.problemValues
                }
            });
            router.off('pointAdd');
            router.on('pointAdd', function(point) {
                self.form.problemValues.push(point);
            });
            router.off('pointDelete');
            router.on('pointDelete', function(point) {
                for(var i = 0; i < self.form.problemValues.length; i++) {
                    if(point.x == self.form.problemValues[i].x
                        && point.y == self.form.problemValues[i].y) {
                        self.form.problemValues.splice(i, 1);
                        break;
                    }
                }
            });
    	},
    	saveTemporary: function() {
    		var self = this;
    		self.$f7.preloader.show();
    		let promises = [];
    		self.deletePhotos.forEach(photo => {
    	        let promise = FileUtil.remove(photo).then(() => {
    	        	return Promise.resolve();
    	        }).catch((error) => {
    	        	console.error(error);
    	        	return Promise.resolve();
    	        });
    	        promises.push(promise);
    		});
    		Promise.all(promises).then(() => {
    			let promises = [];
    			self.form.photos.forEach(photo => {
    				let promise = null;
    				if(photo.indexOf(Common.directory().localeCheck) < 0) {
    					let splits = photo.split('/');
    					let filename = splits[splits.length - 1];
    					let to = Common.directory().base + Common.directory().localeCheck + filename;
    					promise = FileUtil.copy(photo, to);
    				} else {
    					promise = new Promise((resolve, reject) => {
    						resolve(photo);
    					});
    				}
                    promises.push(promise);
    			});
    			return Promise.all(promises);
    		}).then((paths) => {
    			let photos = [];
    			paths.forEach(path => {
    				photos.push(path);
    			});
    			self.form.photos = photos;
    			self.form.place = self.place;
    			let sql = '';
    			if(self.form.tid == null) {
    				sql = "\
                        insert into tmp_locale_check_checkquality_problem_regist( \
                        batch_id, building_id, unit_id, floor_id, house_id, place, problem_class_id, \
                        problem_id, regist, remark, emergency_degree, repair_deadline, contractor_id, \
                        repair, reinspects, ccs, photos, problem_values) \
                        values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) \
                    ";
    			} else {
    				sql = "\
                        update tmp_locale_check_checkquality_problem_regist \
                        set batch_id = ?, building_id = ?, unit_id = ?, floor_id = ?, house_id = ?, \
                        place = ?, problem_class_id = ?, problem_id = ?, regist = ?, remark = ?, \
                        emergency_degree = ?, repair_deadline = ?, contractor_id = ?, \
                        repair = ?, reinspects = ?, ccs = ?, photos = ?, problem_values = ? \
                        where tid = '" + self.form.tid + "' \
                    ";
    			}
    			var values = [
    				self.form.batchId,
    				self.form.buildingId,
    				self.form.unitId,
    				self.form.floorId,
    				self.form.houseId,
    				self.form.place,
    				self.form.problemClassId,
    				self.form.problemId,
    				self.form.regist,
    				self.form.remark,
    				self.form.emergencyDegree,
    				self.form.repairDeadline,
    				self.form.contractorId,
    				self.form.repair,
                    JSON.stringify(self.form.reinspects),
                    JSON.stringify(self.form.ccs),
                    JSON.stringify(self.form.photos),
                    JSON.stringify(self.form.problemValues)
                ];
    			return Common.executeSql(sql, values);
    		}).then((result) => {
                self.$f7.dialog.alert('', '暂存问题成功');
                self.$f7router.emit("saveTemporary", self.form.tid);
                self.$f7.preloader.hide();
                self.$f7router.back();
    		}).catch(error => {
    			console.error(error);
    			self.$f7.dialog.alert('', '暂存问题失败');
    			self.$f7.preloader.hide();
    		});
    	},
    	submit: function() {
    		if(!Common.isOnline()) {
    			this.$f7.dialog.alert('', '当前网络信号较弱，本问题已暂存草稿!');
    			this.saveTemporary();
    			return;
    		}
    		 if(this.form.problemClassId == null) {
    			this.$f7.dialog.alert('', '请选择检查项!');
    			return;
    		}
    		if(!this.isProblemClassHighlight) {
    			if(this.form.repair == null || this.form.repair == '') {
                    this.$f7.dialog.alert('', '请选择整改人!');
                    return;
    			}
                if(this.form.reinspects.length == 0) {
                    this.$f7.dialog.alert('', '请选择复验人!');
                    return;
                }
            }
            this.problemRegist(this.form);
    	},
        changeBuilding: function(buildingId) {
        	if(this.form.buildingId == buildingId) {
        		this.form.buildingId = null;
        	} else {
        		this.form.buildingId = buildingId;
        	}
        }
    },
    computed: {
    	buildings() {
            var buildings = [];
            for(var buildingId in this.$store.state.global.buildingMap) {
                var building = this.$store.state.global.buildingMap[buildingId];
                if(building.bid_section_id == this.batch.bidSectionId) {
                    buildings.push(building);
                }
            }
            return buildings.sort(function(a, b) {
                return parseInt(a.building_name) - parseInt(b.building_name);
            });
    	},
    	units() {
            var units = [];
            for(var unitId in this.$store.state.global.unitMap) {
                var unit = this.$store.state.global.unitMap[unitId];
                if(unit.building_id == this.form.buildingId) {
                    units.push(unit);
                }
            }
            return units.sort(function(a, b) {
                return parseInt(a.unit_name) - parseInt(b.unit_name);
            });
    	},
    	floors() {
            var floors = [];
            for(var floorId in this.$store.state.global.floorMap) {
                var floor = this.$store.state.global.floorMap[floorId];
                if(floor.building_id == this.form.buildingId) {
                    floors.push(floor);
                }
            }
            return floors.sort(function(a, b) {
                return parseInt(a.floor_name) - parseInt(b.floor_name);
            });
    	},
    	houses() {
    		var houses = [];
    		if(this.form.floorId == null || this.form.unitId == null) {
    			return houses;
    		}
    		houses = this.$store.state.global.houseMap.floorUnitMap[this.form.floorId + this.form.unitId].concat();
            return houses.sort(function(a, b) {
                return parseInt(a.house_name) - parseInt(b.house_name);
            });
    	},
    	checkItems() {
            var checkItems = [];
            let bidSectionCheckItemIds = 
                this.$store.state.global.bidSectionCheckItemRel.bidSectionMap[this.batch.bidSectionId];
            let problemClasses = [];
            for(var i = 0; i < bidSectionCheckItemIds.length; i++) {
            	let bidSectionCheckItemId = bidSectionCheckItemIds[i];
                var bidSectionCheckItem = this.$store.state.global.bidSectionCheckItemRel[bidSectionCheckItemId];
                var problemClass = this.$store.state.global.problemClassMap[bidSectionCheckItem.check_item_id];
                if(problemClass.type == '现场检查' || problemClass.type == '31') {
                	problemClasses.push(problemClass);
                }
            }
            problemClasses.sort((p1, p2) => {
            	return p1.level - p2.level;
            });
            for(var i = 0; i < problemClasses.length; i++) {
            	let problemClass = problemClasses[i];
                var checkItem = {};
                checkItem.id = problemClass.id;
                checkItem.name = problemClass.problem_class_name;
                checkItem.type = 'PROBLEM_CLASS';
                checkItem.children = [];
                checkItem.contractor = bidSectionCheckItem.contractor_id;
                checkItem.repair = bidSectionCheckItem.repair;
                checkItem.reinspects = 
                    (bidSectionCheckItem.reinspect == null || bidSectionCheckItem.reinspect == '') 
                    ? [] : bidSectionCheckItem.reinspect.split(',');
                checkItem.ccs = 
                    (bidSectionCheckItem.cc == null || bidSectionCheckItem.cc == '')
                    ? [] : bidSectionCheckItem.cc.split(',');
                this.checkItemMap[checkItem.id] = checkItem;
                if(problemClass.level == 2) {
                    checkItems.push(checkItem);
                } else {
                    var parentCheckItem = this.checkItemMap[problemClass.parent_problem_class_id];
                    parentCheckItem.children.push(checkItem);
                }
            }
            for(var problemId in this.$store.state.global.problemMap) {
                var problem = this.$store.state.global.problemMap[problemId];
                var parentCheckItem = this.checkItemMap[problem.class_id];
                if(parentCheckItem != null) {
                    var checkItem = {};
                    checkItem.id = problem.id;
                    checkItem.name = problem.problem_name;
                    checkItem.type = 'PROBLEM'; 
                    checkItem.children = [];
                    this.checkItemMap[checkItem.id] = checkItem;
                    parentCheckItem.children.push(checkItem);
                }
            }
            return checkItems;
    	},
    	place() {
    		if(this.form.buildingId == null) {
    			return '请选择';
    		}
            let place = '';
            if(this.form.buildingId != null) {
                place += this.$store.state.global.buildingMap[this.form.buildingId].building_name;
            }
            if(this.form.unitId != null) {
                place += this.$store.state.global.unitMap[this.form.unitId].unit_name + '单元';
            }
            if(this.form.floorId != null) {
                place += this.$store.state.global.floorMap[this.form.floorId].floor_name + '层';
            }
            if(this.form.houseId != null) {
                place += this.$store.state.global.houseMap[this.form.houseId].house_name;
            }
            return place;
    	},
        isProblemClassHighlight: function() {
        	if(this.form.problemClassId == null) {
        		return false;
        	}
        	var parentProblemClass =
        		this.$store.state.global.problemClassMap[
        			this.$store.state.global.problemClassMap[this.form.problemClassId].parent_problem_class_id
        		];
        	if(parentProblemClass.problem_class_name == '工程亮点') {
        		return true;
        	} else {
        		return false;
        	}
        }
    },
    watch: {
        'form.contractorId': function(newVal, oldVal) {
            var str = this.form.contractorId == null ? '' : 
                this.$store.state.global.contractorMap[this.form.contractorId].contractor_short_name;
            this.$f7.smartSelect.get('#repairContractorSmartSelect .smart-select').setValue(str);
        },
        'form.repair': function(newVal, oldVal) {
            var str = this.form.repair == null ? '' : 
                this.$store.state.global.userMap[this.form.repair].realname
            this.$f7.smartSelect.get('#repairUserSmartSelect .smart-select').setValue(str);
        },
        'form.reinspects': function(newVal, oldVal) {
            var self = this;
            var reinspectNames = this.form.reinspects.map(function(reinspect) {
                return self.$store.state.global.userMap[reinspect].realname;
            });
            this.$f7.smartSelect.get('#reinspectUserSmartSelect .smart-select').setValue(
                reinspectNames.join(',')
            );
        },
        'form.ccs': function(newVal, oldVal) {
        	var self = this;
            var ccNames = this.form.ccs.map(function(cc) {
                return self.$store.state.global.userMap[cc].realname;
            });
            this.$f7.smartSelect.get('#ccUserSmartSelect .smart-select').setValue(
                ccNames.join(',')
            );
        }
    },
    created: function() {
    	var self = this;
    	this.isAlert = true;
    	this.backMessage = '你确定要退出吗？';
        var localeCheck = getStore(keys.localeCheckProblemRegistKey);
        if(localeCheck == null || localeCheck.bidSectionId == null || 
        	    localeCheck.bidSectionId != this.batch.bidSectionId) {
            clearStore(keys.localeCheckProblemRegistKey);
            localeCheck = {};
            localeCheck.bidSectionId = this.batch.bidSectionId;
            setStore(keys.localeCheckProblemRegistKey, localeCheck);
        } 
        this.form.buildingId = localeCheck.buildingId;
        this.form.unitId = localeCheck.unitId;
        this.form.floorId = localeCheck.floorId;
    },
    mounted: function() {
        var self = this;                
        self.form.tid = this.draftProblem.tid;
        self.form.batchId = this.draftProblem.batchId;
        self.form.buildingId = this.draftProblem.buildingId;
        self.form.unitId = this.draftProblem.unitId;
        self.form.floorId = this.draftProblem.floorId;
        self.form.houseId = this.draftProblem.houseId;
        self.form.problemClassId = this.draftProblem.problemClassId;
        self.form.problemId = this.draftProblem.problemId;
        self.form.remark = this.draftProblem.remark;
        self.form.emergencyDegree = this.draftProblem.emergencyDegree;
        self.form.repairDeadline = this.draftProblem.repairDeadline;
        self.form.regist = this.draftProblem.regist;
        self.form.contractorId = this.draftProblem.contractorId;
        self.form.repair = this.draftProblem.repair;
        self.form.reinspects = this.draftProblem.reinspects;
        self.form.ccs = this.draftProblem.ccs;
        self.form.problemValues = this.draftProblem.problemValues;
        self.form.photos = this.draftProblem.photos;
        self.$refs.refTakePhoto.addPhotos(self.form.photos);
        self.$watch('form.buildingId', function(newVal, oldVal) {
            var localeCheck = getStore(keys.localeCheckProblemRegistKey);
            localeCheck.buildingId = this.form.buildingId;
            localeCheck.unitId = null;
            localeCheck.floorId = null;
            setStore(keys.localeCheckProblemRegistKey, localeCheck);
            this.form.unitId = null;
        });
        self.$watch('form.unitId', function(newVal, oldVal) {
            var localeCheck = getStore(keys.localeCheckProblemRegistKey);
            localeCheck.unitId = this.form.unitId;
            localeCheck.floorId = null;
            setStore(keys.localeCheckProblemRegistKey, localeCheck);
            this.form.floorId = null;
        });
        self.$watch('form.floorId', function(newVal, oldVal) {
            var localeCheck = getStore(keys.localeCheckProblemRegistKey);
            localeCheck.floorId = this.form.floorId;
            setStore(keys.localeCheckProblemRegistKey, localeCheck);
            this.form.houseId = null;
        });
        self.$watch('form.houseId', function(newVal, oldVal) {
            this.form.problemValues = [];
            if(newVal != null) {
                this.openHousePoint(this.$store.state.global.houseMap[this.form.houseId]);
            }
        });
        self.$watch('form.problemClassId', function(newVal, oldVal) {
            var self = this;
            this.form.problemId = null;
            if(this.form.problemClassId != null) {
                var checkItem = this.checkItemMap[this.form.problemClassId];
                this.form.contractorId = checkItem.contractor;
                this.$f7.smartSelect.get('#repairContractorSmartSelect .smart-select').setValue(
                    this.form.contractorId == null ? '' : 
                        this.$store.state.global.contractorMap[this.form.contractorId].contractor_short_name
                );
                this.form.repair = checkItem.repair;
                this.$f7.smartSelect.get('#repairUserSmartSelect .smart-select').setValue(
                    this.form.repair == null ? '' : 
                        this.$store.state.global.userMap[this.form.repair].realname
                );
                this.form.reinspects = checkItem.reinspects;
                this.form.reinspects.push(getUser().userId);
                var reinspectNames = this.form.reinspects.map(function(reinspect) {
                    return self.$store.state.global.userMap[reinspect].realname;
                });
                this.$f7.smartSelect.get('#reinspectUserSmartSelect .smart-select').setValue(
                    reinspectNames.join(',')
                );
            }
        });
        self.$watch('form.problemId', function(newVal, oldVal) {
            
        });
        self.$f7.preloader.hide();
    }
}
</script>

<style scoped>

</style>